@echo off
chcp 65001 >nul
title Uninstall Dependencies Tool

echo ================================================
echo Builder.Alyr.Net Dependencies Uninstaller
echo (c) 2020-2025 ALYR.NET
echo ================================================
echo.

cd /d "%~dp0"

echo Starting dependency cleanup process...
echo.

echo Checking project environment...
if not exist "package.json" (
    echo ERROR: package.json file not found!
    echo Please run this script in project root directory
    pause
    exit /b 1
)

echo Project environment check passed
echo.

echo WARNING: This will remove ALL dependencies and node_modules!
echo This action cannot be undone.
echo.
set /p confirm="Are you sure you want to continue? (Y/N): "
if /i not "%confirm%"=="Y" (
    echo Operation cancelled by user
    pause
    exit /b 0
)

echo.
echo Starting cleanup process...
echo.

rem Remove node_modules directory
if exist "node_modules" (
    echo Removing node_modules directory...
    rmdir /s /q "node_modules"
    if exist "node_modules" (
        echo WARNING: Some files in node_modules could not be deleted
        echo This might be due to file locks or permissions
    ) else (
        echo node_modules directory removed successfully
    )
) else (
    echo node_modules directory not found, skipping...
)

rem Remove config/node_modules directory
if exist "config\node_modules" (
    echo Removing config/node_modules directory...
    rmdir /s /q "config\node_modules"
    if exist "config\node_modules" (
        echo WARNING: Some files in config/node_modules could not be deleted
        echo This might be due to file locks or permissions
    ) else (
        echo config/node_modules directory removed successfully
    )
) else (
    echo config/node_modules directory not found, skipping...
)

rem Remove package-lock.json
if exist "package-lock.json" (
    echo Removing package-lock.json...
    del /f /q "package-lock.json"
    echo package-lock.json removed successfully
) else (
    echo package-lock.json not found, skipping...
)

rem Remove yarn.lock if exists
if exist "yarn.lock" (
    echo Removing yarn.lock...
    del /f /q "yarn.lock"
    echo yarn.lock removed successfully
) else (
    echo yarn.lock not found, skipping...
)

rem Remove .install.lock if exists
if exist ".install.lock" (
    echo Removing .install.lock...
    del /f /q ".install.lock"
    echo .install.lock removed successfully
) else (
    echo .install.lock not found, skipping...
)

rem Clear npm cache
echo.
echo Clearing npm cache...
npm cache clean --force
if errorlevel 1 (
    echo WARNING: Failed to clear npm cache
) else (
    echo npm cache cleared successfully
)

echo.
echo ================================================
echo Dependency cleanup completed successfully!
echo ================================================
echo.
echo What was removed:
echo - node_modules directory
echo - package-lock.json file
echo - yarn.lock file if existed
echo - .install.lock file if existed
echo - npm cache cleared
echo.
echo To reinstall dependencies, run:
echo - npm install for npm
echo - yarn install for yarn
echo - Or use the install script
echo.
pause